function request(url, params,method='GET',toptis) {
  let header = null
  let timeout = 10000
  let tokenName = 'key'
  let disidkey='disidkey'
  let p = new Promise((resolve, reject,) => {
    // 开启login
    if(toptis){
      wx.showLoading({
        title: toptis,
        mask:true,
      })
    }
    wx.showNavigationBarLoading()
    let token = wx.getStorageSync(tokenName)
    params.key = token
    params.disid = wx.getStorageSync(disidkey)
    // 请求
    wx.request({
      url: url,
      data: params,
      timeout,
      header: header || {
        'Content-Type': 'application/json'
      },
      method,
      success: (res) => {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            resolve(res.data.datas)
          } else {
            console.log(res.data.datas.error)
            if (res.data.datas.error == "请登录") {
              let pageArr = getCurrentPages();
              let thisPage = pageArr[pageArr.length - 1]
              wx.removeStorageSync(tokenName)
              thisPage.setData({
                register: true,
              })
            } else {
             
              console.log(res.data.datas.error)
              wx.showToast({
                title: res.data.datas.error,
                icon: 'none'
              })
            }
            reject(res)

          }
        }

      },
      fail: err => {
        reject(err)
      },
      complete: () => {
        if(toptis){
          wx.hideLoading({
          
          })
        }
        wx.hideNavigationBarLoading()
      }

    })
  })
  return p
}

function get(url, params={},toptis){
return  request(url, params,'GET',toptis)
}
function post(url, params={},toptis){
return  request(url, params,'POST',toptis)
}
function http(url, params,method='GET',toptis){
return  request(url, params,method,toptis)
}
http.get =get 
http.post=post


export default http